package main

import (
	"flag"
	"fmt"
	"log"
	"path/filepath"
	"time"

	"github.com/opencloudos/onyx/internal/indexer"
)

func main() {
	layerPath := flag.String("layer", "", "Path to layer tar or tar.gz file")
	blockSize := flag.Int("block-size", 4*1024*1024, "Block size in bytes (default 4MB)")
	output := flag.String("output", "", "Output index file (default: layer.tar.onyxidx.json)")
	flag.Parse()

	if *layerPath == "" {
		log.Fatal("Error: must specify --layer path")
	}

	if *output == "" {
		base := filepath.Base(*layerPath)
		*output = fmt.Sprintf("%s.onyxidx.json", base)
	}

	log.Printf("[onyx-indexer] Building index for %s ...", *layerPath)

	idx, err := indexer.BuildLayerIndex(*layerPath, *blockSize)
	if err != nil {
		log.Fatalf("Indexing failed: %v", err)
	}

	idx.CreatedAt = time.Now().Format(time.RFC3339)
	idx.Version = "v1"

	if err := indexer.SaveIndex(idx, *output); err != nil {
		log.Fatalf("Failed to write index: %v", err)
	}

	log.Printf("[onyx-indexer] ✅ Done. Index saved to %s", *output)
}
